package com.lxl.mybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxl.mybatisplus.entity.Book;
import com.lxl.mybatisplus.service.BookService;
import com.lxl.mybatisplus.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class BookController {

    @Autowired
    private BookService bookService;



    @GetMapping("/books")
    public ModelAndView findAllBooks(){
        System.out.println("books----");
        List<Book> books=bookService.findAllBooks();

        ModelAndView mv=new ModelAndView();
        mv.addObject("books",books);
        mv.setViewName("books");
        return mv;
    }

    @GetMapping("/books/{id}")
    public ModelAndView findStudentById(@PathVariable("id") int id){
        Book book=bookService.findBookById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("book",book);
        mv.setViewName("book");
        return mv;

    }

    @PostMapping("/addBook")
    public ModelAndView addStudent(Book book){
        bookService.addBook(book);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:/books");//添加成功后，跳转到查找所有学生的控制器
        return mv;
    }

    @GetMapping("/addBook")
    public String addStudent(){
        return "addBook";
    }

    @GetMapping("/deleteBook/{id}")
    public ModelAndView deleteStudent(@PathVariable("id") int id){
        bookService.deleteBook(id);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:/books");//删除成功后，跳转到查找所有学生的控制器
        return mv;
    }

    @GetMapping("/updateBook/{id}")
    public ModelAndView toUpdateStudent(@PathVariable("id") int id){
        Book book=bookService.findBookById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("book",book);
        mv.setViewName("updateBook");
        return mv;
    }

    @PostMapping("/updateBook")
    public ModelAndView UpdateStudent(Book book){

        bookService.updateBook(book);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:/books");//修改成功后，跳转到查找所有学生的控制器
        return mv;
    }

    @GetMapping("/booksPage")
    public ModelAndView booksPage(@RequestParam(value="start",defaultValue = "1")int start,
                                  @RequestParam(value="size",defaultValue = "5") int size){ //默认查询第1页，每页显示3条
        IPage<Book> page=bookService.getPage(start,size); //表示起始页为start,每页显示size条记录，根据id升序排序进行分页
        ModelAndView mv=new ModelAndView();
        mv.addObject("page",page);
        mv.setViewName("booksPage");
        return mv;
    }

    @GetMapping("/searchBooks")
    public ModelAndView searchBooks(Book book){
        List<Book> books=bookService.searchBooks(book);
        ModelAndView mv=new ModelAndView();
        mv.addObject("books",books);
        mv.setViewName("books");
        return mv;
    }

    @Autowired
    private IBookService iBookService;
    @GetMapping("/books2")
    public ModelAndView findAllBooks2(){
        List<Book> books=iBookService.list();
        ModelAndView mv=new ModelAndView();
        mv.addObject("books",books);
        mv.setViewName("books");
        return mv;
    }

    @GetMapping("/books2/{id}")
    public ModelAndView findBooksById2(@PathVariable int id){
        Book book=iBookService.getById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("book",book);
        mv.setViewName("book");
        return mv;
    }


}
